/*-----------------------------------------------------------------------------------*/
/* Nuvoton Technology Corporation confidential                                       */
/*                                                                                   */
/* Copyright (c) 2008 by Nuvoton Technology Corporation                              */
/* All rights reserved                                                               */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
/* File Contents:                                                                    */
/*   rtc_lib.c                                                                       */
/*            The RTC Library.                                                       */
/*                                                                                   */
/* Version:                                                                          */
/*            v0.0.1                                                                 */
/*                                                                                   */
/* Project:                                                                          */
/*            			                                                             */
/*                                                                                   */
/*                                                        $Date: 08/08/22 9:14p $   */
/*-----------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of system headers                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
/*---------------------------------------------------------------------------------------------------------*/
/* Includes of local headers                                                                               */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
#define RTC_GLOBALS
#include "nuc900_rtc.h"

/*---------------------------------------------------------------------------------------------------------*/
/* Global file scope (static) variables                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
static RTC_FUNC_T  *fnCallBack_Tick    = NULL,
                   *fnCallBack_Alarm   = NULL,
                   *fnCallBack_Wakeup  = NULL;
static UINT32 volatile uRTC_Count  = 0;
static CHAR _RTC_cHourMode = 0;
static BOOL volatile bIsEnableTickInt  = FALSE;
static BOOL volatile bIsEnableAlarmInt = FALSE;
static BOOL volatile bIsEnableWakeupInt= FALSE;

static UINT32 volatile uReg,uReg1,hiYear,loYear,hiMonth,loMonth,hiDay,loDay;
static UINT32 volatile hiHour,loHour,hiMin,loMin,hiSec,loSec;
/*---------------------------------------------------------------------------------------------------------*/
/* Prototypes of static functions                                                                          */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Functions                                                                                               */
/*---------------------------------------------------------------------------------------------------------*/

//#define RTC_DEBUG
#ifdef RTC_DEBUG
#define RTCDEBUG     SYS_Printf
#else
#define RTCDEBUG(...)
#endif

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     <RTC_ISR>                                                                                 */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               VOID                                                                                      */
/* Returns:                                                                                                */
/*               None                                                                                      */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* Description:                                                                                            */
/*               Install ISR to handle interrupt event                                                     */
/*---------------------------------------------------------------------------------------------------------*/

VOID RTC_ISR (VOID)
{
    UINT32 volatile uRegRIIR;
    

    uRegRIIR = inpw(REG_RTC_RIIR);
    if (uRegRIIR & TICK_INT)                                     /* tick interrupt occurred                */
    {
          outpw(REG_RTC_RIIR, (UINT)uRegRIIR);
          uRTC_Count++;                                            /* maintain RTC tick count              */
   
          if (fnCallBack_Tick != NULL)                             /* execute tick callback function       */
          {
              fnCallBack_Tick();
             
          } 
          
  		  outpw(REG_RTC_RIIR,inpw(REG_RTC_RIIR)&~TICK_INT);

    }

    if (uRegRIIR & ALARM_INT)                                     /* alarm interrupt occurred              */
    {
          outpw(REG_RTC_RIIR, (UINT)uRegRIIR);
          if (fnCallBack_Alarm != NULL)                            /* execute alarm callback function      */
          {
              fnCallBack_Alarm();
              
              
          }
          RTC_Ioctl(0,RTC_IOC_DISABLE_INT,ALARM_INT,0);
        
		  outpw(REG_RTC_RIIR,inpw(REG_RTC_RIIR)&~ALARM_INT);
      
    }

    if (uRegRIIR & WAKEUP_INT)                                   /* wakeup interrupt occurred              */
    {
          outpw(REG_RTC_RIIR, (UINT)uRegRIIR);
         
          if (fnCallBack_Wakeup != NULL)                           /* execute wakeup callback function     */
          {
              fnCallBack_Wakeup();
          }

          RTC_Ioctl(0,RTC_IOC_DISABLE_INT,WAKEUP_INT,0);
          
          outpw(REG_RTC_RIIR,inpw(REG_RTC_RIIR)&~WAKEUP_INT);
    }
    
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_SetFrequecyCompenation                                                                */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               float number                                                                              */
/* Returns:                                                                                                */
/*               0                      Success.                                                           */
/*               RTC_ERR_FCR_VALUE      Compenation VALUE                                                  */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*                                                                                                         */
/*               Set Frequecy Compenation Data                                                             */
/*---------------------------------------------------------------------------------------------------------*/
static INT RTC_SetFrequecyCompenation(float number)
{
    INT intergerPart  ;
    INT fractionPart ;
    INT32 uRegInt,uRegFra ; 
 
    intergerPart = (INT) (number) ; 
    fractionPart = ((INT) ( ((number - intergerPart) *100)) / 100);
    uRegInt = intergerPart - RTC_FCR_REFERENCE ;
    uRegFra = ((fractionPart) * 60); 
    /*-----------------------------------------------------------------------------------------------------*/
    /* Judge Interger part is reasonable                                                                   */
    /*-----------------------------------------------------------------------------------------------------*/
   
    if ( uRegInt < 0 | uRegInt > 15)
    {
        return RTC_ERR_FCR_VALUE ;
    }

    outpw(REG_RTC_FCR, (UINT)(uReg >>8 | uRegFra));

    return Successful;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_WriteEnable                                                                           */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               VOID                                                                                      */
/* Returns:                                                                                                */
/*               0            Success.                                                                     */
/*               1            Fail                                                                         */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*                                                                                                         */
/*               Access PW to AER to make access other register enable                                     */
/*---------------------------------------------------------------------------------------------------------*/

static INT RTC_WriteEnable (VOID)
{
    INT i;

    outpw(REG_RTC_AER, WRITE_KEY);
    

    for (i = 0 ; i < WAIT_COUNT ; i++)
    {
        /*-------------------------------------------------------------------------------------------------*/
        /* check RTC_AER[16] to find out RTC write enable                                                  */
        /*-------------------------------------------------------------------------------------------------*/
        if ( inpw(REG_RTC_AER) && 0x10000000 )
        {
            break;
        }    
    }

    if (i == WAIT_COUNT)
    {
        RTCDEBUG ("\nRTC: RTC_WriteEnable, set write enable Fail!\n");
        return Fail;
    }

    return Successful;
}


/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_Init                                                                                  */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               VOID                                                                                      */
/* Returns:                                                                                                */
/*               0            Success.                                                                     */
/*               RTC_ERR_EIO      Initial RTC Fail.                                                        */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*                                                                                                         */
/*               Initial RTC and install ISR                                                               */
/*---------------------------------------------------------------------------------------------------------*/


INT RTC_Init (VOID)
{
    INT i;
  
    /*-----------------------------------------------------------------------------------------------------*/
    /* Initial time data struct and some parameters.                                                       */
    /*-----------------------------------------------------------------------------------------------------*/
    fnCallBack_Alarm = NULL;
    fnCallBack_Tick = NULL;
    fnCallBack_Wakeup = NULL;
    
    uRTC_Count = 0;
    /*-----------------------------------------------------------------------------------------------------*/
    /* When RTC is power on, write 0xa5eb1357 to RTC_INIR to reset all logic.                              */
    /*-----------------------------------------------------------------------------------------------------*/
    outpw(REG_RTC_INIR, INIT_KEY);

    for (i = 0 ; i < WAIT_COUNT ; i++)
    {
        if ( inpw(REG_RTC_INIR) & 0x01 )
        {            /* Check RTC_INIR[0] to find out RTC reset signal                                     */
            break;

        }
    }

    if (i == WAIT_COUNT)
    {
        RTCDEBUG("\nRTC: RTC_Init, initial RTC Fail!\n");
        return RTC_ERR_EIO;
    }

    /*-----------------------------------------------------------------------------------------------------*/
    /* Install RTC ISR / enable CPSR I bit                                                                 */
    /*-----------------------------------------------------------------------------------------------------*/
   
    sysInstallISR(IRQ_LEVEL_1, IRQ_RTC, (PVOID)RTC_ISR);
    sysEnableInterrupt(IRQ_RTC);
    sysSetLocalInterrupt(ENABLE_IRQ);
    
    return Successful;
}


/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_Read                                                                                  */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               uCmd                    Currnet_Timer/ Alarm_Time                                         */
/*               RTC_TIME_DATA_T *pt     Time Data                                                         */
/* Returns:                                                                                                */
/*               0                       Success.                                                          */
/*               RTC_ERR_EIO                 Initial RTC Fail.                                             */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*               Read current date/time or alarm date/time from RTC                                        */
//*--------------------------------------------------------------------------------------------------------*/

INT RTC_Read (TIME_SELECT time, RTC_TIME_DATA_T *pt)
{

    pt->ucClockDisplay = inpw(REG_RTC_TSSR);                               /* 12/24-hour                   */
    pt->ucDayOfWeek = inpw(REG_RTC_DWR);                                   /* Day of week                  */

    switch (time)
    {
        case CURRENT_TIME:
        {
			uReg   = inpw(REG_RTC_CLR);
            uReg1  = inpw(REG_RTC_TLR);
            break;
		}
        case ALARM_TIME:
        {
			uReg   = inpw(REG_RTC_CAR);
            uReg1  = inpw(REG_RTC_TAR);
            break;
        }    
        default:
        {
            return RTC_ERR_ENOTTY;
        }    
    }


    hiYear  = (uReg & 0xF00000) >> 20;
    loYear  = (uReg & 0xF0000) >> 16;
    hiMonth = (uReg & 0x1000) >> 12;
    loMonth = (uReg & 0xF00) >> 8;
    hiDay   = (uReg & 0x30) >> 4;
    loDay   =  uReg & 0xF;

    pt->uYear   = (hiYear * 10)  + loYear + RTCYEAR2000;
    pt->ucMonth = (hiMonth * 10) + loMonth;
    pt->ucDay   = (hiDay * 10)   + loDay;


    hiHour = (uReg1 & 0x300000) >> 20;
    loHour = (uReg1 & 0xF0000) >> 16;
    hiMin  = (uReg1 & 0x7000) >> 12;
    loMin  = (uReg1 & 0xF00) >> 8;
    hiSec  = (uReg1 & 0x70) >> 4;
    loSec  =  uReg1 & 0xF;


    if (pt->ucClockDisplay == RTC_CLOCK_12)
    {
        pt->ucHour = (hiHour * 10) + loHour;                                /* AM: 1~12. PM: 21~32.        */

        if (pt->ucHour >= 21)
        {
            pt->ucAmPm = RTC_PM;
            pt->ucHour -= 20;
        }
        else
        {
            pt->ucAmPm = RTC_AM;
        }
        pt->ucMinute = (hiMin  * 10) + loMin ;
        pt->ucSecond = (hiSec  * 10) + loSec ;
        
    }
    else
    {                                                                      /* RTC_CLOCK_24                 */
        pt->ucHour   = (hiHour * 10) + loHour;
        pt->ucMinute = (hiMin  * 10) + loMin ;
        pt->ucSecond = (hiSec  * 10) + loSec ;
    }


    return Successful;

}



/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_Write                                                                                 */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               uCmd                    Currnet_Timer/ Alarm_Time                                         */
/*               RTC_TIME_DATA_T *pt     Time Data                                                         */
/* Returns:                                                                                                */
/*               0                       Success.                                                          */
/*               RTC_ERR_EIO                 Initial RTC Fail.                                             */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*               Set current date/time or alarm date/time from RTC                                        */
//*--------------------------------------------------------------------------------------------------------*/
INT RTC_Write(TIME_SELECT time, RTC_TIME_DATA_T *pt)
{
    /*-----------------------------------------------------------------------------------------------------*/
    /* Check RTC time data value is reasonable or not.                                                     */
    /*-----------------------------------------------------------------------------------------------------*/
    if ( ((pt->uYear - RTCYEAR2000) > 99)|
         ((pt->ucMonth == 0) || (pt->ucMonth > 12))|
         ((pt->ucDay   == 0) || (pt->ucDay   > 31)))
    {
        RTCDEBUG ("\nRTC: Year value is incorrect\n");
        return Fail;
    }

    if ( (pt->uYear - RTCYEAR2000) > 99 )
    {
        RTCDEBUG ("\nRTC: Year value is incorrect\n");
        return Fail;
    }

    if ( (pt->ucMonth == 0) || (pt->ucMonth > 12) )
    {
        RTCDEBUG ("\nRTC: Month value is incorrect\n");
        return Fail;
    }

    if ( (pt->ucDay == 0) || (pt->ucDay > 31) )
    {
        RTCDEBUG ("\nRTC: Day value is incorrect\n");
        return Fail;
    }

    if (pt->ucClockDisplay == RTC_CLOCK_12)
    {
        if ( (pt->ucHour == 0) || (pt->ucHour > 12) )
        {
            RTCDEBUG ("\nRTC: Hour value is incorrect\n");
            return Fail;
        }
    }
    else if (pt->ucClockDisplay == RTC_CLOCK_24)
    {
        if (pt->ucHour > 23)
        {
            RTCDEBUG ("\nRTC: Hour value is incorrect\n");
            return Fail;
        }
    }
    else
    {
        RTCDEBUG ("\nRTC: Clock mode is incorrect\n");
        return Fail;
    }

    if (pt->ucMinute > 59)
    {
        RTCDEBUG ("\nRTC: Minute value is incorrect\n");
        return Fail;
    }

    if (pt->ucSecond > 59)
    {
        RTCDEBUG ("\nRTC: Second value is incorrect\n");
        return Fail;
    }

    if (pt->ucDayOfWeek > 6)
    {
        RTCDEBUG ("\nRTC: Day of week value is incorrect\n");
        return Fail;
    }


    /*-----------------------------------------------------------------------------------------------------*/
    /* Important, call RTC_Open() before write data into any register.                                     */
    /*-----------------------------------------------------------------------------------------------------*/
    uReg = RTC_WriteEnable();
    if (uReg != 0)
    {
        return Fail;
    }

    switch (time)
    {

        case CURRENT_TIME:
            /*---------------------------------------------------------------------------------------------*/
            /* Second, set RTC time data.                                                                  */
            /*---------------------------------------------------------------------------------------------*/

            if (pt->ucClockDisplay == RTC_CLOCK_12)
            {
                _RTC_cHourMode = RTC_CLOCK_12;
                outpw(REG_RTC_TSSR, RTC_CLOCK_12);
                RTCDEBUG ("RTC: 12-hour\n");
                /*-----------------------------------------------------------------------------------------*/
                /* important, range of 12-hour PM mode is 21 upto 32                                       */
                /*-----------------------------------------------------------------------------------------*/
                if (pt->ucAmPm == RTC_PM)
                    pt->ucHour += 20;
            }
            else                                                                  /* RTC_CLOCK_24          */
            {
                _RTC_cHourMode = RTC_CLOCK_24;
                outpw(REG_RTC_TSSR, RTC_CLOCK_24);
                RTCDEBUG ("RTC: 24-hour\n");
            }

            outpw(REG_RTC_DWR,(UINT) pt->ucDayOfWeek);

            hiYear  = (pt->uYear - RTCYEAR2000) / 10;
            loYear  = (pt->uYear - RTCYEAR2000) % 10;
            hiMonth = pt->ucMonth / 10;
            loMonth = pt->ucMonth % 10;
            hiDay   = pt->ucDay / 10;
            loDay   = pt->ucDay % 10;
            uReg    = (hiYear << 20) | (loYear << 16) | (hiMonth << 12) | (loMonth << 8) | (hiDay << 4) | loDay;
            RTCDEBUG ("RTC: REG_RTC_CLR[0x%08x]\n", uReg);
            RTC_WriteEnable();
            outpw (REG_RTC_CLR, (UINT)uReg);

            hiHour  = pt->ucHour / 10;
            loHour  = pt->ucHour % 10;
            hiMin   = pt->ucMinute / 10;
            loMin   = pt->ucMinute % 10;
            hiSec   = pt->ucSecond / 10;
            loSec   = pt->ucSecond % 10;
            uReg    = (hiHour << 20) | (loHour << 16) | (hiMin << 12) | (loMin << 8) | (hiSec << 4) | loSec;
            RTCDEBUG ("RTC: REG_RTC_TLR[0x%08x]\n", uReg);
			RTC_WriteEnable();
            outpw(REG_RTC_TLR, (UINT)uReg);

            return Successful;
            break;

         case ALARM_TIME:

            fnCallBack_Alarm = NULL;                                         /* Initial call back function.*/
            /*---------------------------------------------------------------------------------------------*/
            /* Second, set alarm time data.                                                                */
            /*---------------------------------------------------------------------------------------------*/
            hiYear = (pt->uYear - RTCYEAR2000) / 10;
            loYear = (pt->uYear - RTCYEAR2000) % 10;
            hiMonth = pt->ucMonth / 10;
            loMonth = pt->ucMonth % 10;
            hiDay = pt->ucDay / 10;
            loDay = pt->ucDay % 10;
            uReg = (hiYear << 20) | (loYear << 16) | (hiMonth << 12) | (loMonth << 8) | (hiDay << 4) | loDay;
            outpw(REG_RTC_CAR, (UINT)uReg);

            if (_RTC_cHourMode == RTC_CLOCK_12)
            {
                if (pt->ucAmPm == RTC_PM)       /* important, range of 12-hour PM mode is 21 upto 32       */
                    pt->ucHour += 20;
            }
            hiHour   = pt->ucHour / 10;
            loHour   = pt->ucHour % 10;
            hiMin  = pt->ucMinute / 10;
            loMin  = pt->ucMinute % 10;
            hiSec  = pt->ucSecond / 10;
            loSec  = pt->ucSecond % 10;
            uReg = (hiHour << 20) | (loHour << 16) | (hiMin << 12) | (loMin << 8) | (hiSec << 4) | loSec;
            outpw(REG_RTC_TAR, (UINT)uReg);
            /*---------------------------------------------------------------------------------------------*/
            /* Third, install alarm callback function.                                                     */
            /*---------------------------------------------------------------------------------------------*/
            if (pt->fnAlarmCallBack != NULL)
                fnCallBack_Alarm = pt->fnAlarmCallBack;
            /*---------------------------------------------------------------------------------------------*/
            /* Finally, enable alarm interrupt.                                                            */
            /*---------------------------------------------------------------------------------------------*/

            RTC_Ioctl(0,RTC_IOC_ENABLE_INT,ALARM_INT,0);
            return Successful;
            break;

       default:
        {
            return RTC_ERR_ENOTTY;
        }   
    }
    return Successful;
}



/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_Ioctl                                                                                 */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               nNum             Interface number.   always set 0                                         */
/*               uCmd             Command.                                                                 */
/*               uArg0            Arguments for the command.                                               */
/*               uArg1            Arguments for the command.                                               */
/* Returns:                                                                                                */
/*               0                Success.                                                                 */
/*               RTC_ERR_ENOTTY        Command not support, or parameter incorrect.                        */
/*               RTC_ERR_ENODEV        Interface number incorrect.                                         */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*               Support some commands for application.                                                    */
//*--------------------------------------------------------------------------------------------------------*/

INT RTC_Ioctl (INT nNum, RTC_CMD uCmd, UINT32 uArg0, float uArg1)
{
    INT nRet;
    UINT32 uReg;
    RTC_TICK_T *ptick;

    if (nNum != 0)
        return RTC_ERR_ENODEV;

    switch (uCmd)
    {

        case RTC_IOC_IDENTIFY_LEAP_YEAR:
        {
            uReg = inpw(REG_RTC_LIR);
            if (uReg & 0x01)
            {
                *(PUINT32)uArg0 = RTC_LEAP_YEAR;
                RTCDEBUG("\nRTC: It's a leap year\n");
            }
            else
            {
                *(PUINT32)uArg0 = 0;
                RTCDEBUG("\nRTC: It's not a leap year\n");
            }
            break;
        }
        case RTC_IOC_SET_TICK_MODE:
        {
            ptick = (RTC_TICK_T *) uArg0;
            RTC_Ioctl(0,RTC_IOC_DISABLE_INT,TICK_INT,0);
            uRTC_Count = 0;

            uReg = RTC_WriteEnable();
            if (uReg != 0)
            {
                return RTC_ERR_EIO ;
            }
            if (ptick->ucMode > RTC_TICK_1_128_SEC)                            /*Tick mode 0 to 7          */
            {
                return RTC_ERR_ENOTTY ;
            }
            if (ptick->fnTickCallBack != NULL)
            {
                fnCallBack_Tick = ptick->fnTickCallBack;
            }
            else
            {
                fnCallBack_Tick = NULL;
            }
            outpw(REG_RTC_TTR, ptick->ucMode);
            /*---------------------------------------------------------------------------------------------*/
            /* Reset tick interrupt status if program enable tick interrupt before.                        */
            /*---------------------------------------------------------------------------------------------*/
            if (bIsEnableTickInt== TRUE)
            {

                RTC_Ioctl(0,RTC_IOC_ENABLE_INT,TICK_INT,0);
                return Successful;
            }
            break;
        }
        
        case RTC_IOC_GET_TICK:
        {
			return uRTC_Count;
        }
        
        case RTC_IOC_RESTORE_TICK:
        {
            uRTC_Count = 0;
            break;
        }
        
        case RTC_IOC_ENABLE_INT:
        {
            switch ((INT_SOURCE)uArg0)
            {
                INT nRet;

                nRet = RTC_WriteEnable();
                {
                    return RTC_ERR_EIO;
                }

                case TICK_INT:
                {
                    bIsEnableTickInt   = TRUE;
                    outpw(REG_RTC_RIER, inpw(REG_RTC_RIER) | TICK_INT)  ;
                    break;
                }
                case ALARM_INT:
                {
                    bIsEnableAlarmInt  = TRUE;
                    outpw(REG_RTC_RIER, inpw(REG_RTC_RIER) | ALARM_INT) ;
                    break;
                }
                case WAKEUP_INT:
                {
                    bIsEnableWakeupInt = TRUE;
                    outpw(REG_RTC_RIER, inpw(REG_RTC_RIER) | WAKEUP_INT);
                    break;
                }
                default:
                {
                    return RTC_ERR_ENOTTY;
                    break;
                }     
            }
            break;
        }
        case RTC_IOC_DISABLE_INT:
        {
            switch ((INT_SOURCE)uArg0)
            {
                INT nRet;

                nRet = RTC_WriteEnable();
                if (nRet != 0)
                {
                    return RTC_ERR_EIO;
                }

                case TICK_INT:
                {
                    bIsEnableTickInt   = FALSE;
                    outpw( REG_RTC_RIER, inpw(REG_RTC_RIER) & (~TICK_INT) );
                    //outpw( REG_RTC_RIIR, inpw(REG_RTC_RIIR) & (TICK_INT) );
                    break;
                }
                case ALARM_INT:
                {
                    bIsEnableAlarmInt  = FALSE;
                    outpw( REG_RTC_RIER, inpw(REG_RTC_RIER) & (~ALARM_INT) );
                    //outpw( REG_RTC_RIIR, inpw(REG_RTC_RIIR) & (ALARM_INT) );
                    break;
                }
                case WAKEUP_INT:
                {
                    bIsEnableWakeupInt = FALSE;
                    outpw( REG_RTC_RIER, inpw(REG_RTC_RIER) & (~WAKEUP_INT) );
                    //outpw( REG_RTC_RIIR, inpw(REG_RTC_RIIR) & (WAKEUP_INT) );
                    break;
                } 
                case ALL_INT:
                {
                    bIsEnableTickInt   = FALSE;
                    bIsEnableAlarmInt  = FALSE;
                    bIsEnableWakeupInt = FALSE;
                    outpw( REG_RTC_RIER, 0 );
                    outpw( REG_RTC_RIIR, ALL_INT );
                    
                    break;
                } 
                default:
                {
                    return RTC_ERR_ENOTTY;
                }       
            }

            
            break;
        }

/*
        case RTC_IOC_SET_ALARM_MASK:
        { 
            RTC_WriteEnable();
            outpw(REG_RTC_RIER,uArg0);
            break;
        }
*/
        case RTC_IOC_SET_FREQUENCY:
        {
            nRet= RTC_SetFrequecyCompenation(uArg0) ;
            if (nRet != 0)
            {
                return RTC_ERR_ENOTTY;
            }
            break;
        }
        
        default:
        {
            return RTC_ERR_ENOTTY;
        }    
    }

    return Successful;
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     RTC_Close                                                                                 */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*               nNum            Interface number.                                                         */
/* Returns:                                                                                                */
/*               0                Success.                                                                 */
/*               RTC_ERR_ENODEV        Interface number incorrect.                                         */
/* Side effects:                                                                                           */
/*                                                                                                         */
/* DESCRIPTION                                                                                             */
/*               Diable VIC channel of RTC and both tick and alarm interrupt..                             */
//*--------------------------------------------------------------------------------------------------------*/

INT RTC_Close (VOID)
{

    bIsEnableTickInt = FALSE;
    sysDisableInterrupt(IRQ_RTC);
    RTC_Ioctl(0,RTC_IOC_DISABLE_INT,ALL_INT,0);
   

    return Successful;
}

extern INT RTC_Init(VOID);   
extern INT RTC_Open(RTC_TIME_DATA_T *pt);
extern INT RTC_Ioctl(INT nNum, RTC_CMD uCmd, UINT32 uArg0, float uArg1);
extern INT RTC_Read(TIME_SELECT time, RTC_TIME_DATA_T *pt);
extern INT RTC_Write(TIME_SELECT time, RTC_TIME_DATA_T *pt);
extern INT RTC_Close(VOID);
